/*
 * File: current_ctl_loop.c
 *
 * Code generated for Simulink model 'current_ctl_loop'.
 *
 * Model version                  : 3.0
 * Simulink Coder version         : 24.1 (R2024a) 19-Nov-2023
 * C/C++ source code generated on : Mon Jun 16 13:50:09 2025
 *
 * Target selection: ert.tlc
 * Embedded hardware selection: ARM Compatible->ARM Cortex
 * Code generation objectives:
 *    1. Execution efficiency
 *    2. Traceability
 * Validation result: Not run
 */

#include "current_ctl_loop.h"
#include "rtwtypes.h"
#include "current_ctl_pi.h"
#include "deadband_comp.h"

/* instance parameters */
InstP _rt_InstP =
{
    {
        /*
         * current_ctl_loop/current_loop_ctl
         *   current_ctl_pi:bw
         */
        200.0F,

        /*
         * current_ctl_loop/current_loop_ctl
         *   current_ctl_pi:dc_bus_limit
         */
        24.0F,

        /*
         * current_ctl_loop/current_loop_ctl
         *   current_ctl_pi:fs
         */
        20000.0F,

        /*
         * current_ctl_loop/current_loop_ctl
         *   current_ctl_pi:ld
         */
        0.000345F,

        /*
         * current_ctl_loop/current_loop_ctl
         *   current_ctl_pi:lq
         */
        0.000345F,

        /*
         * current_ctl_loop/current_loop_ctl
         *   current_ctl_pi:r
         */
        0.976F
    }
};

/* Block signals and states (default storage) */
DW _rt_DW;

/* External inputs (root inport signals with default storage) */
ExtU _rt_U;

/* External outputs (root outports fed by signals with default storage) */
ExtY _rt_Y;

/* Real-time model */
static RT_MODEL _rt_M_;
RT_MODEL *const _rt_M = &_rt_M_;

/* Model step function for TID0 */
void current_ctl_loop_step0(void)      /* Sample time: [1.0E-5s, 0.0s] */
{
    /* ModelReference: '<Root>/current_loop_ctl' */
    current_ctl_piTID0();

    /* ModelReference: '<Root>/ua_comp' */
    deadband_compTID0();

    /* ModelReference: '<Root>/ub_comp' */
    deadband_compTID0();

    /* ModelReference: '<Root>/uc_comp' */
    deadband_compTID0();
}

/* Model step function for TID1 */
void current_ctl_loop_step1(void)      /* Sample time: [5.0E-5s, 0.0s] */
{
    /* MATLAB Function: '<Root>/mod2pi_fun' incorporates:
     *  Inport: '<Root>/elec_theta_rad'
     */
    _rt_DW.n_ang = _rt_U.elec_theta_rad;

    /* MATLAB Function 'fast_math/mod2pi_fun': '<S1>:1' */
    /* '<S1>:1:3' TWO_PI = single(2*pi); */
    /* '<S1>:1:4' n_ang = single(ang); */
    /* '<S1>:1:5' while n_ang < 0 */
    while (_rt_DW.n_ang < 0.0F)
    {
        /* '<S1>:1:6' n_ang = n_ang + TWO_PI; */
        _rt_DW.n_ang += 6.28318548F;
    }

    /* '<S1>:1:8' while n_ang >= TWO_PI */
    while (_rt_DW.n_ang >= 6.28318548F)
    {
        /* '<S1>:1:9' n_ang = n_ang - TWO_PI; */
        _rt_DW.n_ang -= 6.28318548F;
    }

    /* End of MATLAB Function: '<Root>/mod2pi_fun' */

    /* ModelReference: '<Root>/current_loop_ctl' incorporates:
     *  Inport: '<Root>/iabc_now_A'
     *  Inport: '<Root>/idq_tar_A'
     *  Outport: '<Root>/idq_now_A'
     */
    current_ctl_piTID1(&(_rt_DW.current_loop_ctl_InstanceData.rtm),
                       &_rt_U.idq_tar_A[0], &_rt_U.iabc_now_A[0], &_rt_DW.n_ang,
                       &_rt_DW.sv_uabc[0], &_rt_Y.idq_now_A[0],
                       &(_rt_DW.current_loop_ctl_InstanceData.rtdw));

    /* ModelReference: '<Root>/ua_comp' incorporates:
     *  Inport: '<Root>/comp_du_V'
     *  Inport: '<Root>/i_noise_A'
     *  Inport: '<Root>/iabc_now_A'
     */
    deadband_compTID1(&_rt_DW.sv_uabc[0], &_rt_U.iabc_now_A[0], &_rt_U.i_noise_A,
                      &_rt_U.comp_du_V, &_rt_DW.u_tar_comp);

    /* ModelReference: '<Root>/ub_comp' incorporates:
     *  Inport: '<Root>/comp_du_V'
     *  Inport: '<Root>/i_noise_A'
     *  Inport: '<Root>/iabc_now_A'
     */
    deadband_compTID1(&_rt_DW.sv_uabc[1], &_rt_U.iabc_now_A[1], &_rt_U.i_noise_A,
                      &_rt_U.comp_du_V, &_rt_DW.u_tar_comp_o);

    /* ModelReference: '<Root>/uc_comp' incorporates:
     *  Inport: '<Root>/comp_du_V'
     *  Inport: '<Root>/i_noise_A'
     *  Inport: '<Root>/iabc_now_A'
     */
    deadband_compTID1(&_rt_DW.sv_uabc[2], &_rt_U.iabc_now_A[2], &_rt_U.i_noise_A,
                      &_rt_U.comp_du_V, &_rt_DW.u_tar_comp_n);

    /* Outport: '<Root>/uabc_tar_comp_V' */
    _rt_Y.uabc_tar_comp_V[0] = _rt_DW.u_tar_comp;
    _rt_Y.uabc_tar_comp_V[1] = _rt_DW.u_tar_comp_o;
    _rt_Y.uabc_tar_comp_V[2] = _rt_DW.u_tar_comp_n;
}

/* Model initialize function */
void current_ctl_loop_initialize(void)
{
    /* Registration code */

    /* Set task counter limit used by the static main program */
    (_rt_M)->Timing.TaskCounters.cLimit[0] = 1;
    (_rt_M)->Timing.TaskCounters.cLimit[1] = 5;

    /* Assign pointer for instance parameters, Block: '<Root>/current_loop_ctl' */
    _rt_DW.current_loop_ctl_InstanceData.rtm.current_ctl_pi_InstP_ref =
        &_rt_InstP.current_ctl_loop_rt_current_loop_ctl;

    /* Model Initialize function for ModelReference Block: '<Root>/current_loop_ctl' */
    current_ctl_pi_initialize(rtmGetErrorStatusPointer(_rt_M),
        &(_rt_DW.current_loop_ctl_InstanceData.rtm));

    /* Model Initialize function for ModelReference Block: '<Root>/ua_comp' */
    deadband_comp_initialize(rtmGetErrorStatusPointer(_rt_M),
        &(_rt_DW.ua_comp_InstanceData.rtm));

    /* Model Initialize function for ModelReference Block: '<Root>/ub_comp' */
    deadband_comp_initialize(rtmGetErrorStatusPointer(_rt_M),
        &(_rt_DW.ub_comp_InstanceData.rtm));

    /* Model Initialize function for ModelReference Block: '<Root>/uc_comp' */
    deadband_comp_initialize(rtmGetErrorStatusPointer(_rt_M),
        &(_rt_DW.uc_comp_InstanceData.rtm));

    /* SystemInitialize for ModelReference: '<Root>/current_loop_ctl' */
    current_ctl_pi_Init(&(_rt_DW.current_loop_ctl_InstanceData.rtdw));
}

/*
 * File trailer for generated code.
 *
 * [EOF]
 */
